<?php

class Tbl_coupon_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_all($limit, $uri) {

        $result = $this->db->get('tbl_coupon', $limit, $uri);
        if ($result->num_rows() > 0) {
            return $result->result_array();
        } else {
            return array();
        }
    }

    function get_one($id) {
        $this->db->where('id', $id);
        $result = $this->db->get('tbl_coupon');
        if ($result->num_rows() == 1) {
            return $result->row_array();
        } else {
            return array();
        }
    }
	
	function get($id = FALSE)
    {
        if ($id === FALSE)
        {
            $query = $this->db->get('tbl_coupon');
            return $query->result_array();
        }
        $query = $this->db->get_where('tbl_coupon', array('id'=>$id));
        return $query->row_array();        
    }
	
	function get_where($where = array())
			{
				return $this->db->get_where('tbl_coupon', $where);
			}

    function insert() {
           $data = array(
        
            'coupon_code' => $this->input->post('coupon_code', TRUE),
           
            'start_date' => date('Y-m-d H:i:s',strtotime($this->input->post('start_date', TRUE))),
           
            'expired_date' => date('Y-m-d H:i:s',strtotime($this->input->post('expired_date', TRUE))),
           
            'jenis_coupon' => $this->input->post('jenis_coupon', TRUE),
           
            'nilai_coupon' => $this->input->post('nilai_coupon', TRUE),
           
            'limit_coupon_used' => $this->input->post('limit_coupon_used', TRUE),
           
            'minimum_price' => $this->input->post('minimum_price', TRUE),
           
            'cat_product' => $this->input->post('cat_product', TRUE),
           
        );
        $this->db->insert('tbl_coupon', $data);
    }
	
	function edit($data, $where){
		$this->db->update('tbl_coupon', $data, $where);
	}

    function update($id) {
        $data = array(
         
       'coupon_code' => $this->input->post('coupon_code', TRUE),
       
            'start_date' => date('Y-m-d H:i:s',strtotime($this->input->post('start_date', TRUE))),
           
            'expired_date' => date('Y-m-d H:i:s',strtotime($this->input->post('expired_date', TRUE))),
       
       'jenis_coupon' => $this->input->post('jenis_coupon', TRUE),
       
       'nilai_coupon' => $this->input->post('nilai_coupon', TRUE),
       
       'limit_coupon_used' => $this->input->post('limit_coupon_used', TRUE),
       
       'minimum_price' => $this->input->post('minimum_price', TRUE),
       
       'cat_product' => $this->input->post('cat_product', TRUE),
       
        );
        $this->db->where('id', $id);
        $this->db->update('tbl_coupon', $data);
    }

    function delete($id) {
        foreach ($id as $sip) {
            $this->db->where('id', $sip);
            $this->db->delete('tbl_coupon');
        }
    }

}
?>
